/**
 * Copyright © 2015 eqxiu.com 北京中网易企秀科技有限公司  All rights reserved.
 */
package com.eqxiu.ieditor.api.impl;


import com.eqxiu.ieditor.api.ITagServiceApi;
import com.eqxiu.ieditor.dao.TagMapper;
import com.eqxiu.ieditor.dto.TagDTO;
import com.eqxiu.ieditor.entity.Tag;
import com.eqxiu.ieditor.entity.TagExample;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @description 标签
 * @author ranger
 * @date Jun 1, 2017
 */
@Service
public class TagServiceApiImpl implements ITagServiceApi {

	@Autowired
	private TagMapper tagMapper;

	@Override
	public List<TagDTO> getTagsByBizType(byte bizType) {
		TagExample example = new TagExample();
		example.createCriteria().andBizTypeEqualTo(bizType);
		example.setOrderByClause("sort desc");
		List<Tag> tags = tagMapper.selectByExample(example);
		List<TagDTO> dtos= Lists.newArrayList();
		if (Objects.nonNull(tags)) {
			for (Tag tag : tags) {
				TagDTO dto = new TagDTO();
				BeanUtils.copyProperties(tag, dto);
				dtos.add(dto);
			}
		}

		return dtos;
	}

	@Override
	public TagDTO getOne(Integer id) {
		Tag tag = tagMapper.selectByPrimaryKey(id);
		TagDTO dto = new TagDTO();
		if (tag!=null){
			BeanUtils.copyProperties(tag, dto);
		}
		return dto;
	}

	@Override
	public Map<String, Object> findByParams(Map<String, String> params) {
		String name = params.get("name");
		String bizType = params.get("bizType");
		Integer pageNumber = Integer.valueOf(params.get("pageNumber"));
		Integer pageSize = Integer.valueOf(params.get("pageSize"));

		TagExample example = new TagExample();
		TagExample.Criteria crit = example.createCriteria();
		example.setOrderByClause("sort desc");
		if (Objects.nonNull(name)) {
			crit.andNameLike("%" + name + "%");
		}
		if (Objects.nonNull(bizType) && !"0".equals(bizType)) {
			crit.andBizTypeEqualTo(Byte.valueOf(params.get("bizType")));
		}

		Page<Tag> tagPage = PageHelper.startPage(pageNumber, pageSize).doSelectPage(() -> {
			tagMapper.selectByExample(example);
		});
		List<Tag> orders = tagPage.getResult();

		List<TagDTO> tagDTOs = orders.stream().map(one -> {
			TagDTO dto = new TagDTO();
			BeanUtils.copyProperties(one, dto);
			return dto;
		}).collect(Collectors.toList());
		Map<String, Object> map = new HashMap<>();
		map.put("total", tagPage.getTotal());
		map.put("rows", tagDTOs);
		return map;
	}

	@Override
	public List<TagDTO> selectByCondition(TagDTO tagDTO) {
		TagExample example = new TagExample();
		TagExample.Criteria criteria = example.createCriteria();
		if (tagDTO.getId()!=null){
			criteria.andIdEqualTo(tagDTO.getId());
		}
		List<Tag> tags = tagMapper.selectByExample(example);
		if (!Objects.isNull(tags)) {
			return tags.stream().map(one -> {
				TagDTO dto = new TagDTO();
				BeanUtils.copyProperties(one, dto);
				return dto;
			}).collect(Collectors.toList());
		}

		return null;
	}

	@Override
	public void save(TagDTO params) {
		Tag record = new Tag();
		BeanUtils.copyProperties(params, record);
		tagMapper.insertSelective(record);
	}

	@Override
	@Transactional
	public void deleteByIds(String ids) {
		Stream.of(ids.split(";")).forEach(one -> {
			tagMapper.deleteByPrimaryKey(Integer.parseInt(one));
		});
	}

	@Override
	public void update(TagDTO params) {
		Tag tag = new Tag();
		BeanUtils.copyProperties(params, tag);
		tagMapper.updateByPrimaryKeySelective(tag);
	}


}
